﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace HZ.Supervise.Web.Peculiar
{
    public partial class HopitalSortSource : BasePage
    {
        int totalrecord = 0;
        DataTable dt = new DataTable();
       static Dictionary<string, string> hid = new Dictionary<string, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dataBind();
               
            }
        }
        public HopitalSortSource() 
        {
            this.ModuleNo = "特殊权限";
            this.PageOperateType = "医院采购维护";
        }
        /// <summary>
        /// url编码
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string UrlEncode(object obj)
        {
            if (obj != null)
            {
                return Server.UrlEncode(obj.ToString());
            }
            else
            {
                return "";
            }

        }
        /// <summary>
        /// 数据查询与绑定
        /// </summary>
        private void dataBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append(" and AreaID like '" + CurrentAccount.GetUser().Org.Area.AreaID.TrimEnd('0') + "%'");
            if (!string.IsNullOrEmpty(txtHospitalname.Text.Trim()))
            {
                where.Append(" and HospitalName like '%" + txtHospitalname.Text.Trim() + "%'");
            }
            if (!droSort.SelectedValue.Equals("-1"))
            {
                  where.Append(" and HSortSource= "+droSort.SelectedValue.ToString());
            }
            if (!string.IsNullOrEmpty(txtAreaNname.Text.Trim()))
            {
                where.Append(" and AreaName like '%" + txtAreaNname.Text.Trim() + "%'");
            }
            HZ.Supervise.IBLL.IBLL_SpecialAuthority sp = HZ.Supervise.BLLFactory.IBLL_SpecialAuthority();
            dt = sp.findHopitalAreaSortSourceByWhere(where.ToString(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalrecord);
            if (hid.Count > 0) hid.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                hid.Add(dt.Rows[i]["Hid"].ToString(), dt.Rows[i][4].ToString());
            }
            gvwProduceCatalog.DataSource = dt;
            gvwProduceCatalog.DataBind();
            AspNetPager1.RecordCount = totalrecord;
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            dataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            dataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<HZ.Supervise.Model.View.HopitalAreaSortSource> list= new List<HZ.Supervise.Model.View.HopitalAreaSortSource>();         
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                HZ.Supervise.Model.View.HopitalAreaSortSource has = new HZ.Supervise.Model.View.HopitalAreaSortSource();
                CheckBox cb = (CheckBox)gvwProduceCatalog.Rows[i].Cells[3].FindControl("cbkIsUsering");
                int jj = HZ.Common.HzConvert.ToInt32(gvwProduceCatalog.Rows[i].Cells[5].Text.Trim())??0;
               if (!hid[(jj).ToString()].Equals(cb.Checked.ToString()))
                {
                    has.Hid = jj;
                    has.HospitalName = gvwProduceCatalog.Rows[i].Cells[0].Text;
                    has.AreaName = gvwProduceCatalog.Rows[i].Cells[1].Text;
                    has.SortSource = gvwProduceCatalog.Rows[i].Cells[6].Text;
                    has.IsUsering = cb.Checked ? 1 : 0;
                    has.Huserid = CurrentAccount.GetUser().UserID;
                    has.Hchangetime = System.DateTime.Now;
                    list.Add(has);
                }  
            }
            if (list.Count > 0)//有修改
            {
                HZ.Supervise.IBLL.IBLL_SpecialAuthority sp = HZ.Supervise.BLLFactory.IBLL_SpecialAuthority();
                int ns = sp.UpdateHopitalAreaSortSource(list);
                if (ns > 0)
                {
                    HZ.Web.WebUtil.Alert("修改成功！");
                }
                else
                {
                    HZ.Web.WebUtil.Alert("修改失败！");
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("没有修改,无需保存！");
            }
            dataBind();
        }
        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProduceCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("View"))
            {
                HZ.Web.WebUtil.WriteUserDefinedScript(string.Format("open('Lookhistory.aspx?id1={0}&id2=0');", e.CommandArgument.ToString()), this);
            }
        }

        
    }
}
